package com.wy2.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wy2.entity.Module;
import com.wy2.entity.Permission;
import com.wy2.service.PermissionService;
import com.wy2.util.FenyeUtil;

@RestController
@RequestMapping("/per")
@CrossOrigin
public class PermissionController {
	@Autowired
	private PermissionService ps;
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 显示权限模块的树结构
	 * 
	 * @return
	 */
	@RequestMapping("/getAll")
	public Object getAll(Permission per) {
		return ps.getPermission(per);
	}
	
	
	/**
	 * 查询角色拥有的增删改权限
	 * @param mid
	 * @return
	 */
	@RequestMapping("/getById")
	public List<Integer> getByMid(String rid) {
		return ps.getById(rid);
	}
	/**
	 * 获取全部
	 * @param fenye
	 * @param permission
	 * @return
	 */
	@RequestMapping("/all")
	@ResponseBody
	public FenyeUtil<Permission> getAll(FenyeUtil<Permission> fenye,Permission permission){
		fenye.setT(permission);
		fenye=ps.getAll(fenye);
		System.out.println(fenye);
		return fenye;
	}
	/**
	 * 添加
	 * @param per
	 * @return
	 */
	@RequestMapping("/add")
	public Permission add(Permission per) {
		String shijian = sdf.format(new Date());
		per.setPlastupdatetime(shijian);
		return ps.add(per);
	}
	/**
	 * 修改
	 * @param per
	 * @return
	 */
	@RequestMapping("/update")
	public Integer update(Permission per) {
		String shijian = sdf.format(new Date());
		per.setPlastupdatetime(shijian);
		return ps.update(per);
	}
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/del")
	public Integer del(String ids) {
		return ps.del(ids);
	}
	
	/**
	 * 删除权限角色中间表的数据
	 * @param rid
	 * @param pid
	 * @return
	 */
	@RequestMapping("/addRidAndPId")
	public Map<String, Object> deleteRid(String rid, @RequestParam("pid") List<String> pid) {
		return ps.deleteRidPid(rid, pid);
	}
	
	// 查询角色拥有的增删改权限
	@RequestMapping("/getUserByPerId")
	public List<Permission> getUserByPerId(String id) {
		return ps.getAll(id);
	}
	
	// 查询改用户是否有此权限
	@RequestMapping("/getAllByPvalue")
	public List<Permission> getAllByPvalue(String id,String pvalue) {
		return ps.getAllByPvalue(id, pvalue);
	}
	
	// 获取点击权限模块的数据
	@RequestMapping("/getPer")
	public Permission getPer(String id) {
		return ps.getPer(id);
	}
}
